//
// Created by Administrator on 2021/10/20.
//
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <climits>

using namespace std;

class Solution {
public:
    int getMinDistance(vector<int> &nums, int target, int start) {
        // 遍历距离
        for (int i = 0; i < nums.size(); ++i) {
            if (i + start < nums.size()) {
                if (nums[i + start] == target)
                    return i;
            }
            if (start - i >= 0) {
                if (nums[start - i] == target)
                    return i;
            }
        }
        return -1;
    }

    int getMinDistance2(vector<int> &nums, int target, int start) {
        int res = nums.size();
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] == target) {
                res = min(res, abs(i - start));
            }
        }
        return res;
    }
};

int main() {
    vector<int> nums{1, 2, 3, 4, 5};
    Solution solution;
    cout << solution.getMinDistance2(nums, 5, 3) << endl;
    return 0;
}
